package com.souku.finance.application.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.bean.BalanceDetailedBean;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.PayUtil;

import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class BalanceDetailedAdapter extends BaseAdapter {

    private List<BalanceDetailedBean.ListBean> beans;

    public BalanceDetailedAdapter(List<BalanceDetailedBean.ListBean> beans) {
        this.beans = beans;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_balancedetailed, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        BalanceDetailedBean.ListBean bean = beans.get(position);
        viewHolder.tvType.setText(PayUtil.getMemo(bean.getType()));
        viewHolder.tvDate.setText(MyTextUtil.changeTimeWithHM(bean.getGmtCreated()));
        viewHolder.tvAccountMoney.setText(MyTextUtil.setMoneyToElement(bean.getAccountMoney()));
        if (Double.parseDouble(bean.getDeltaMoney()) < 0) {
            viewHolder.tvDeltaMoney.setTextColor(parent.getResources().getColor(R.color.main_green));
        } else {
            viewHolder.tvDeltaMoney.setTextColor(parent.getResources().getColor(R.color.main_red));
        }
        viewHolder.tvDeltaMoney.setText(MyTextUtil.setMoneyToElement(bean.getDeltaMoney()));
        return view;
    }


    static class ViewHolder {
        protected TextView tvType;
        protected TextView tvAccountMoney;
        protected TextView tvDate;
        protected TextView tvDeltaMoney;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvType = (TextView) rootView.findViewById(R.id.tv_type);
            tvAccountMoney = (TextView) rootView.findViewById(R.id.tv_accountMoney);
            tvDate = (TextView) rootView.findViewById(R.id.tv_date);
            tvDeltaMoney = (TextView) rootView.findViewById(R.id.tv_deltaMoney);
        }
    }
}
